/**
 * 公共区域
 */
import school from 'src/router/school'
import social from 'src/router/social'
import { getRoleInfo } from 'api/common/index'
import { getBasicInfo } from 'api/school/student/index'
import { getMechaniInfo } from 'api/social/set/index'
/**
 * 权限判断
 * 此函数用于权限判断，一开始过滤父级路由, 如果父级路由存在，过滤子路由，在过滤子路由前先判断当前路由是多层级，如果不是多层级则返回全部子路由
 * @param route 路有对象
 * @param roles 权限对象
 * @param parent
 */
function hasPermission (route, roles, parent) {
  let strFlag = false
  if (parent) {
    if (parent.multiple) {
      roles.some(item => {
        if (item.routeName === route.meta.name) {
          strFlag = true
          return true
        }
      })
    } else {
      strFlag = true
    }
  } else {
    roles.some(item => {
      if (item.routeName === route.meta.name) {
        strFlag = true
        return true
      }
    })
  }
  return strFlag
}

/**
 * 路由表过滤
 * 返回过滤后的路由表
 * @param routes 原始路由表
 * @param roles 角色权限
 * @param parent 父级路由
 */

function filterRouter (routes, roles, parent) {
  let passRoute = routes.filter(item => {
    if (hasPermission(item, roles, parent)) {
      if (item.children && item.children.length) {
        item.children = filterRouter(item.children, roles, item)
      }
      return true
    } else {
      return false
    }
  })
  return passRoute
}

/**
 * 路由文件过滤
 * @param type 系统类型 school(学校) social(社保)
 */
async function filterRouterFile (type, getters) {
  let routeLists = []
  switch (type) {
    case 'school':
      if (getters.getRole === 0) { // 用户管理员登录
        routeLists = school
      } else {
        routeLists = filterRouter(school, getters.getRoleList)
      }
      break
    case 'social':
      routeLists = social
      break
  }
  return routeLists
}

const state = {
  roleData: {}, // 用户信息
  systemInfo: '' // 系统信息
}

const getters = {
  getRole: state => state.roleData.pid || 0, // 获取用户角色
  getNetType: state => { // 获取系统类型
    if (state.roleData.nets) {
      let offset = state.roleData.netsOffset
      return state.roleData.nets[offset].netType
    }
    return ''
  },
  getRoleList: state => {
    let offset = state.roleData.netsOffset
    if (state.roleData.nets) return state.roleData.nets[offset].netMenu || []
  }, // 获取权限列表
  getSystemInfo: state => state.systemInfo // 获取系统信息
}

const mutations = {
  SET_ROLEDATA (state, payload) { // 用户信息
    state.roleData = payload
  },
  SET_SYSTEMINFO (state, payload) { // 设置系统信息
    state.systemInfo = payload
  }
}

const actions = {
  setResetData ({ commit }) { // 重置路由信息
    commit('SET_ROLEDATA', {})
  },
  setRoleData ({ commit, getters }) {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await getRoleInfo()
        commit('SET_ROLEDATA', res.data)
        resolve(filterRouterFile(getters.getNetType, getters))
      } catch (err) {
        reject(err)
      }
    })
  },
  setSystemInfo ({ getters, commit }) { // 此方法用于处理顶部信息与设置模块
    return new Promise(async (resolve, reject) => {
      try {
        let [res, params, net] = [{}, {}, {}]
        if (getters.getNetType === 'school') {
          res = await getBasicInfo()
          net = res.data.school
          params = {
            'net': {
              'name': net.schoolName,
              'logo': net.schoolLogo,
              'address': net.schoolAddress,
              'linkMan': net.linkMan,
              'linkMobile': net.linkMobile
            }
          }
        } else {
          res = await getMechaniInfo()
          net = res.data.social
          params = {
            'net': {
              'name': net.name,
              'logo': net.logo,
              'address': net.address,
              'linkMan': net.linkMan,
              'linkMobile': net.linkMobile
            }
          }
        }
        commit('SET_SYSTEMINFO', {
          ...params,
          'settings': res.data.settings
        })
        resolve({
          ...params,
          'settings': res.data.settings
        })
      } catch (err) {
        reject(err)
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
